/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.jiujiudevice.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boxin.ims.modules.jiujiudevice.dao.JiuCashRecordDao;
import com.boxin.ims.modules.jiujiudevice.entity.JiuCashRecord;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;

/**
 * 押金记录Service
 * 
 * @author sxh
 * @version 2018-08-15
 */
@Component
@Transactional(readOnly = true)
public class JiuCashRecordService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(JiuCashRecordService.class);

	@Autowired
	private JiuCashRecordDao jiuCashRecordDao;

	public JiuCashRecord get(Long id) {
		return jiuCashRecordDao.findOne(id);
	}

	public JiuCashRecord findByMemberId(Long memberId) {
		DetachedCriteria dc = jiuCashRecordDao.createDetachedCriteria();
		dc.add(Restrictions.eq("memberId", memberId));
		List<JiuCashRecord> list = jiuCashRecordDao.find(dc);
		if(list.size()>0) {
			return list.get(0);
		}else {
			
			return null; 
		}
	}
	public JiuCashRecord findByOrderNo(String orderNo) {
		DetachedCriteria dc = jiuCashRecordDao.createDetachedCriteria();
		dc.add(Restrictions.eq("orderNo", orderNo));
		List<JiuCashRecord> list = jiuCashRecordDao.find(dc);
		if(list.size()>0) {
			return list.get(0);
		}else {
			
			return null; 
		}
		
	}
	/**
	 * 删除所有超过十分钟未支付的押金记录
	 * 
	 * @param jiuCashRecord
	 * @return
	 */
	public int deleteExpiredNoPay() {
		String sqlString = "DELETE FROM jiu_cash_record WHERE create_time <= CURRENT_TIMESTAMP - INTERVAL 10 MINUTE AND `status`=0;";
		return jiuCashRecordDao.createSqlQuery(sqlString).executeUpdate();
		
	}
	public List<JiuCashRecord> find(JiuCashRecord jiuCashRecord) {
		DetachedCriteria dc = jiuCashRecordDao.createDetachedCriteria();
		
		if(jiuCashRecord.getMemberId() != null) {
			dc.add(Restrictions.eq("memberId", jiuCashRecord.getMemberId()));
		}
		if(StringUtils.isNotEmpty(jiuCashRecord.getMemberOpenId())) {
			dc.add(Restrictions.eq("memberOpenId", jiuCashRecord.getMemberOpenId()));
		}
		if(jiuCashRecord.getStatus() != null) {
			dc.add(Restrictions.eq("status", jiuCashRecord.getStatus()));
		}
		dc.addOrder(Order.desc("id"));
		return jiuCashRecordDao.find(dc);
	}

	public Page<JiuCashRecord> find(Page<JiuCashRecord> page, JiuCashRecord jiuCashRecord) {
		DetachedCriteria dc = jiuCashRecordDao.createDetachedCriteria();
		if (StringUtils.isNotEmpty(jiuCashRecord.getMemberOpenId())) {
			dc.add(Restrictions.eq("memberOpenId",  jiuCashRecord.getMemberOpenId()));
		}
		if (StringUtils.isNotEmpty(jiuCashRecord.getMemberNickname())) {
			dc.add(Restrictions.like("memberNickname", "%" + jiuCashRecord.getMemberNickname() + "%"));
		}
		if (jiuCashRecord.getStatus() != null) {
			dc.add(Restrictions.eq("status", jiuCashRecord.getStatus()));
		}
		dc.add(Restrictions.ne("status",JiuCashRecord.STATUS_NO_PAY));
		if (jiuCashRecord.getRefundStatus() != null) {
			dc.add(Restrictions.eq("refundStatus", jiuCashRecord.getRefundStatus()));
		}
		if (StringUtils.isNotEmpty(jiuCashRecord.getOrderNo())) {
			dc.add(Restrictions.eq("orderNo", jiuCashRecord.getOrderNo() ));
		}
		if (jiuCashRecord.getPayStatus() != null) {
			dc.add(Restrictions.eq("payStatus",  jiuCashRecord.getPayStatus() ));
		}
		
		dc.addOrder(Order.desc("id"));
		return jiuCashRecordDao.find(page, dc);
	}
	@Transactional(readOnly = false)
	public void save(JiuCashRecord jiuCashRecord) {
		jiuCashRecordDao.save(jiuCashRecord);
	}

	@Transactional(readOnly = false)
	public void delete(Long id) {
		jiuCashRecordDao.deleteById(id);
	}

}
